﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class DispatchDetailDA : IDispatchDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DELIVERY_ORDER_ID = "@DELIVERY_ORDER_ID";
        private string DISPATCH_ID = "@DISPATCH_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_DISPATCH_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@DISPATCH_DETAIL WHERE DISPATCH_ID = @DISPATCH_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_DISPATCH_DETAIL = " DELETE FROM @_@DISPATCH_DETAIL WHERE DISPATCH_ID = @DISPATCH_ID ";
        private string SQL_INSERT_DISPATCH_DETAIL = " INSERT INTO @_@DISPATCH_DETAIL ( WH_ID, DISPATCH_ID, LINE_ID, DELIVERY_ORDER_ID, UDF1, UDF2, UDF3, UDF4, UDF5, UDF6, UDF7, UDF8, UDF9, UDF10, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @DISPATCH_ID, @LINE_ID, @DELIVERY_ORDER_ID, @UDF1, @UDF2, @UDF3, @UDF4, @UDF5, @UDF6, @UDF7, @UDF8, @UDF9, @UDF10, @REMARK, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_DISPATCH_DETAIL = " SELECT  ROWID, WH_ID, DISPATCH_ID, LINE_ID, DELIVERY_ORDER_ID, UDF1, UDF2, UDF3, UDF4, UDF5, UDF6, UDF7, UDF8, UDF9, UDF10, REMARK, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@DISPATCH_DETAIL ";
        private string SQL_UPDATE_DISPATCH_DETAIL = " UPDATE @_@DISPATCH_DETAIL SET WH_ID = @WH_ID, DELIVERY_ORDER_ID = @DELIVERY_ORDER_ID, UDF1 = @UDF1, UDF2 = @UDF2, UDF3 = @UDF3, UDF4 = @UDF4, UDF5 = @UDF5, UDF6 = @UDF6, UDF7 = @UDF7, UDF8 = @UDF8, UDF9 = @UDF9, UDF10 = @UDF10, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE DISPATCH_ID = @DISPATCH_ID AND LINE_ID = @LINE_ID  ";
        private string TS = "@TS";
        private string UDF1 = "@UDF1";
        private string UDF10 = "@UDF10";
        private string UDF2 = "@UDF2";
        private string UDF3 = "@UDF3";
        private string UDF4 = "@UDF4";
        private string UDF5 = "@UDF5";
        private string UDF6 = "@UDF6";
        private string UDF7 = "@UDF7";
        private string UDF8 = "@UDF8";
        private string UDF9 = "@UDF9";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckDispatchDetailIDUnique(string dispatchID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_DISPATCH_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckDispatchDetailIDUnique(DataBase dataBase, DbTransaction tran, string dispatchID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_DISPATCH_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteDispatchDetail(List<string> dispatchDetailID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < dispatchDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchDetailID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteDispatchDetail(List<string> dispatchID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    if (dispatchID.Count != lineID.Count)
                    {
                        throw new Exception("dispatchID和lineID数量不对应!");
                    }
                    for (int j = 0; j < dispatchID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]) + " AND LINE_ID = @LINE_ID", paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteDispatchDetail(DataBase dataBase, DbTransaction tran, List<string> dispatchDetailID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < dispatchDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchDetailID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteDispatchDetail(DataBase dataBase, DbTransaction tran, List<string> dispatchID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    if (dispatchID.Count != lineID.Count)
                    {
                        throw new Exception("dispatchID和lineID数量不对应!");
                    }
                    for (int j = 0; j < dispatchID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]) + " AND LINE_ID = @LINE_ID", paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllDispatchDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllDispatchDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public DispatchDetailInfo GetDispatchDetailByID(string dispatchID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID) + " WHERE DISPATCH_ID = @DISPATCH_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID), new SqlParameter("@LINE_ID", lineID) };
            DispatchDetailInfo dispatchDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    dispatchDetailInfo = new DispatchDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["DISPATCH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DELIVERY_ORDER_ID"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["UDF6"].ToString(), reader["UDF7"].ToString(), reader["UDF8"].ToString(), reader["UDF9"].ToString(), reader["UDF10"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return dispatchDetailInfo;
        }

        public DispatchDetailInfo GetDispatchDetailByID(DataBase dataBase, DbTransaction tran, string dispatchID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID) + " WHERE DISPATCH_ID = @DISPATCH_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DISPATCH_ID", dispatchID), new SqlParameter("@LINE_ID", lineID) };
            DispatchDetailInfo dispatchDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                dispatchDetailInfo = new DispatchDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["DISPATCH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DELIVERY_ORDER_ID"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["UDF6"].ToString(), reader["UDF7"].ToString(), reader["UDF8"].ToString(), reader["UDF9"].ToString(), reader["UDF10"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return dispatchDetailInfo;
        }

        public DataSet GetDispatchDetailByQueryList(List<string> sqlWhere, DispatchDetailQueryEntity dispatchdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!dispatchdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, dispatchdetailQuery.CurrentPage, dispatchdetailQuery.PageSize, dispatchdetailQuery.SortField, dispatchdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetDispatchDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, DispatchDetailQueryEntity dispatchdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, dispatchdetailQuery.CurrentPage, dispatchdetailQuery.PageSize, dispatchdetailQuery.SortField, dispatchdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<DispatchDetailInfo> GetDispatchDetailListByID(string dispatchID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID) + " WHERE DISPATCH_ID = @DISPATCH_ID ORDER BY ROWID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.DISPATCH_ID, dispatchID) };
            List<DispatchDetailInfo> dispatchDetailInfos = new List<DispatchDetailInfo>();
            DispatchDetailInfo dispatchDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    dispatchDetailInfo = new DispatchDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["DISPATCH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DELIVERY_ORDER_ID"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["UDF6"].ToString(), reader["UDF7"].ToString(), reader["UDF8"].ToString(), reader["UDF9"].ToString(), reader["UDF10"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    dispatchDetailInfos.Add(dispatchDetailInfo);
                }
            }
            return dispatchDetailInfos;
        }

        public List<DispatchDetailInfo> GetDispatchDetailListByID(DataBase dataBase, DbTransaction tran, string dispatchID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISPATCH_DETAIL.Replace("@_@", whLoginID) + " WHERE DISPATCH_ID = @DISPATCH_ID ORDER BY ROWID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.DISPATCH_ID, dispatchID) };
            List<DispatchDetailInfo> dispatchDetailInfos = new List<DispatchDetailInfo>();
            DispatchDetailInfo dispatchDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                dispatchDetailInfo = new DispatchDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["DISPATCH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DELIVERY_ORDER_ID"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["UDF6"].ToString(), reader["UDF7"].ToString(), reader["UDF8"].ToString(), reader["UDF9"].ToString(), reader["UDF10"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                dispatchDetailInfos.Add(dispatchDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return dispatchDetailInfos;
        }

        public int InsertDispatchDetail(DispatchDetailInfo dispatchDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    dispatchDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_DispatchDetail_Parameters(dispatchDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertDispatchDetail(DataBase dataBase, DbTransaction tran, DispatchDetailInfo dispatchDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    dispatchDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_DispatchDetail_Parameters(dispatchDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_DispatchDetail_Parameters(DispatchDetailInfo dispatchDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DISPATCH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.DELIVERY_ORDER_ID, SqlDbType.VarChar, 30), new SqlParameter(this.UDF1, SqlDbType.VarChar, 30), new SqlParameter(this.UDF2, SqlDbType.VarChar, 30), new SqlParameter(this.UDF3, SqlDbType.VarChar, 30), new SqlParameter(this.UDF4, SqlDbType.VarChar, 30), new SqlParameter(this.UDF5, SqlDbType.VarChar, 30), new SqlParameter(this.UDF6, SqlDbType.VarChar, 30), new SqlParameter(this.UDF7, SqlDbType.VarChar, 30), new SqlParameter(this.UDF8, SqlDbType.VarChar, 30), new SqlParameter(this.UDF9, SqlDbType.VarChar, 30), new SqlParameter(this.UDF10, SqlDbType.VarChar, 30), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), 
                new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30)
             };
            if (!string.IsNullOrEmpty(dispatchDetailInfo.WhID))
            {
                paramArray[0].Value = dispatchDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = dispatchDetailInfo.DispatchID;
            paramArray[2].Value = dispatchDetailInfo.LineID;
            paramArray[3].Value = dispatchDetailInfo.DeliveryOrderID;
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf1))
            {
                paramArray[4].Value = dispatchDetailInfo.Udf1;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf2))
            {
                paramArray[5].Value = dispatchDetailInfo.Udf2;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf3))
            {
                paramArray[6].Value = dispatchDetailInfo.Udf3;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf4))
            {
                paramArray[7].Value = dispatchDetailInfo.Udf4;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf5))
            {
                paramArray[8].Value = dispatchDetailInfo.Udf5;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf6))
            {
                paramArray[9].Value = dispatchDetailInfo.Udf6;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf7))
            {
                paramArray[10].Value = dispatchDetailInfo.Udf7;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf8))
            {
                paramArray[11].Value = dispatchDetailInfo.Udf8;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf9))
            {
                paramArray[12].Value = dispatchDetailInfo.Udf9;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Udf10))
            {
                paramArray[13].Value = dispatchDetailInfo.Udf10;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(dispatchDetailInfo.Remark))
            {
                paramArray[14].Value = dispatchDetailInfo.Remark;
            }
            else
            {
                paramArray[14].Value = DBNull.Value;
            }
            paramArray[15].Value = dispatchDetailInfo.IsActive;
            paramArray[0x10].Value = dispatchDetailInfo.CreatedBy;
            paramArray[0x11].Value = dispatchDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateDispatchDetail(DispatchDetailInfo dispatchDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    dispatchDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_DispatchDetail_Parameters(dispatchDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateDispatchDetail(DataBase dataBase, DbTransaction tran, DispatchDetailInfo dispatchDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    dispatchDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_DispatchDetail_Parameters(dispatchDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DISPATCH_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

